<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="com.puneMovies.data.Movie" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
function closePopup()
{

 <%if(request.getAttribute("closeWindow")!=null){%>
 var parentWin=window.opener;   
 parentWin.document.location.reload();
 window.close();
 <%}%>
}
function addMovie()
{

 document.forms[0].actionRequest.value='addMovie';
 document.forms[0].movieDescription.value= document.forms[0].movieDescriptionTA.value;
 var actorsInMovie=document.getElementById('actorsInMovie');
 var actorString='';
	for(var i=0;i< actorsInMovie.options.length;i++)
	{
		 if(i < actorsInMovie.options.length-1)
		 {
		  actorString=actorString+actorsInMovie.options[i].value+':';
		 }else
		 {
		  actorString=actorString+actorsInMovie.options[i].value;
		 }
	 
	}
 alert('actorString '+actorString);
 document.forms[0].actorString.value=actorString;
 document.forms[0].submit();
}


function modifyMovie()
{

 document.forms[0].action='modifyMovie.do';
 document.forms[0].actionRequest.value='modifyMovie';
 document.forms[0].movieDescription.value= document.forms[0].movieDescriptionTA.value;
 document.forms[0].submit();


}

function addActorsToMovie()
{
alert('inside addActorsToMovie');
var actorList=document.getElementById('actorList');
var actorsInMovie=document.getElementById('actorsInMovie');
alert('actorList.options.length  '+actorList.options.length);
var indexesToBeDeleted=new Array();
var count=0;
for(var i=0;i< actorList.options.length;i++)
{
 if(actorList.options[i].selected)
 {
   var optionToAdd=new Option(actorList.options[i].text,actorList.options[i].value);
   actorsInMovie.options[actorsInMovie.options.length]=optionToAdd;      
   indexesToBeDeleted[count]=actorList.options[i].value;
   count++;
 }
}
for(var i=0;i< indexesToBeDeleted.length;i++)
{  
  for(var j=0;j< actorList.options.length;j++)
  {
    if(actorList.options[j].value==indexesToBeDeleted[i])
    {
  	  actorList.options[j]=null;   
  	  j--;
  	}
  }
 
}

}


function removeActorsFromMovie()
{
alert('inside removeActorsFromMovie');
var actorList=document.getElementById('actorList');
var actorsInMovie=document.getElementById('actorsInMovie');
alert('actorsInMovie.options.length  '+actorsInMovie.options.length);
var indexesToBeDeleted=new Array();
var count=0;
for(var i=0;i< actorsInMovie.options.length;i++)
{
 if(actorsInMovie.options[i].selected)
 {
  var optionToAdd=new Option(actorsInMovie.options[i].text,actorsInMovie.options[i].value);
  actorList.options[actorList.options.length]=optionToAdd;
  indexesToBeDeleted[count]=actorsInMovie.options[i].value;
   count++;
 }
}

for(var i=0;i< indexesToBeDeleted.length;i++)
{  
  for(var j=0;j< actorsInMovie.options.length;j++)
  {
    if(actorsInMovie.options[j].value==indexesToBeDeleted[i])
    {
  	  actorsInMovie.options[j]=null;   
  	  j--;
  	}
  }
 
}

}

function addNewMovie()
{
 alert('inside addNewMovie ');
 document.getElementById('actionRequest').value='addMovie';
 document.getElementById('movieDescription').value= document.getElementById('movieDescriptionTA').value
 var actorsInMovie=document.getElementById('actorsInMovie');
 for(var i=0;i< actorsInMovie.options.length;i++)
 {
     actorsInMovie.options[i].selected=true;	
 }
 document.forms[0].submit();
}
</script>
</head>

<c:set var="movie" value="${requestScope.Movie_For_Modify}" />

<body onLoad="closePopup()" >
<form name="f1" action="manageMovies.do">
<table width="100%" border="0">
  <tr>
    <td width="29%">Movie Name </td>
    <td width="71%"><input type="text" name="movieName"  
	 value="<c:out value="${movie.movieName}" />" /></td>
  </tr>
  <tr>
    <td>Director</td>
    <td><input type="text" name="director" value="<c:out value="${movie.directorName}" />" /></td>
  </tr>
  <tr>
    <td>Producer Company </td>
    <td><input type="text" name="producerCompany" value="<c:out value="${movie.producerCompany}" />" /></td>
  </tr>
  <tr>
    <td>Actor List </td>
    <td><table width="200" border="0">
        <tr>
          <td width="61">Actors List     
		  
            <select name="actorList" size="6" multiple="multiple">
				<c:forEach var="actor" items="${requestScope.actorList}" >
				<option value="<c:out value="${actor.actorId}" />" >
				<c:out value="${actor.firstName}" />&nbsp;&nbsp;
				<c:out value="${actor.lastName}" />
				</option>
				</c:forEach>
            </select>
			</td>
          <td width="35"><input type="button" name="Submit3" value="&gt;&gt;"  
		   onclick="addActorsToMovie()"/>
            <input type="button" name="Submit32" value="&lt;&lt;"  
		   onclick="removeActorsFromMovie()"/></td>
          <td width="82">Actors In Movie
		   <c:set var="actorsInMovie" value="${movie.actors}" />      
          <select name="actorsInMovie" size="6" multiple="multiple">
		  	<c:forEach var="actor" items="${actorsInMovie}" >
			<option value="<c:out value="${actor.actorId}" />" >
			<c:out value="${actor.firstName}" />&nbsp;&nbsp;
			<c:out value="${actor.lastName}" />
			</option>
			</c:forEach>
          </select>
		  </td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td height="52">Movie Description </td>
    <td>
		<textarea  name="movieDescriptionTA"><c:out value="${movie.movieDescription}" />
		</textarea>
	</td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
    <c:choose>
     <c:when test="${empty theater}" >
	  <input type="button" name="Submit" value="Add Movie"  onClick="addNewMovie()"/>
	 </c:when>
     <c:otherwise>
      <input type="button" name="Submit" value="Submit"  onClick="modifyMovie()"/>
      <input type="hidden" name="movieId" value="${movie.movieId}" />
     </c:otherwise>     
     </c:choose>
      <input type="hidden" name="actionRequest">
      <input type="hidden" name="movieDescription" >
     <input type="hidden" name="actorString" >
      <input type="button" name="cancel" value="Cancel" />
    </div></td>
  </tr>
</table>
</form>
</body>
</html>
